package com.store.bookstore_backend.web.controller;

import com.store.bookstore_backend.domain.User;
import com.store.bookstore_backend.dto.*;
import com.store.bookstore_backend.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;

    @PostMapping("/register")
    public APIResponse<Object> registerUser(@RequestBody UserRegisterDTO userRegisterDTO) {
        userService.userRegister(userRegisterDTO);
        return APIResponse.success();
    }

    @PostMapping("/login")
    public APIResponse<LoginDataDTO> loginUser(@RequestBody UserLoginDTO userLoginDTO) {
        LoginDataDTO loginDataDTO = userService.userLogin(userLoginDTO);
        return APIResponse.success(loginDataDTO);
    }

    @GetMapping("/getUserInfo")
    public APIResponse<User> getUserInfo(HttpServletRequest request) {

        return APIResponse.success(userService.getUserInfo(request));
    }


    //更新密码接口
    @PutMapping("/updatePassword")
    public APIResponse<Object> ChangePassword(HttpServletRequest request, @RequestBody UserUpdatePasswordDTO userUpdatePasswordDTO) {

        userService.UpdatePassWord(request, userUpdatePasswordDTO);
        return APIResponse.success();
    }

    //更新用户信息接口
    @PutMapping("/updateUser")
    public APIResponse<Object> UpdateUser(@RequestBody UserMessageUpdateDTO userMessageUpdateDTO) {
        userService.UpdateUser(userMessageUpdateDTO);
        return APIResponse.success();
    }
}

